package com.juhuacha.tryrxjava.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by Administrator on 2016/8/30.
 * 邮箱       juhuacha18@gmail.com
 * E-Mail     juhuacha18@gmail.com
 * CopyRight  9hgame.com reserved
 */
public class CustomScrollView extends ViewGroup {

    private int mScreenHeight;
    private int mLastY;
    private int mStart;
    private Scroller mScroller;
    private int mEnd;

    public CustomScrollView(Context context) {
        this(context,null,0);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mScroller = new Scroller(context);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int count = getChildCount();
        //设置ViewGroup的高度
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        mlp.height = mScreenHeight * count;
        setLayoutParams(mlp);
        for (int j = 0; j < count; j++) {
            View child = getChildAt(j);
            if(child.getVisibility() != View.GONE){
                child.layout(i,i * mScreenHeight,i2,(i+1)*mScreenHeight);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();

        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            measureChild(childView,widthMeasureSpec,heightMeasureSpec);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //记录上次点击的位置
                mLastY = y;
                //记录点击起点
                mStart = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                if(!mScroller.isFinished()){
                    mScroller.abortAnimation();
                }
                int dy = mLastY - y;
                if(getScrollY() < 0){
                    dy = 0;
                }
                if(getScrollY() > getHeight() - mScreenHeight){
                    dy = 0;
                }
                scrollBy(0,dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if(dScrollY > 0){
                    if(dScrollY < mScreenHeight / 3 ){
                        mScroller.startScroll(
                                0,getScrollY(),
                                0,-dScrollY);
                    }else{
                        mScroller.startScroll(
                                0,getScrollY(),
                                0,mScreenHeight - dScrollY);
                    }
                }else{
                    if(-dScrollY < mScreenHeight / 3 ){
                        mScroller.startScroll(
                                0,getScrollY(),
                                0,-dScrollY);
                    }else{
                        mScroller.startScroll(
                                0,getScrollY(),
                                0,-mScreenHeight - dScrollY);
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mScreenHeight = getMeasuredHeight();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(mScroller.computeScrollOffset()){
            scrollTo(0,mScroller.getCurrY());
            postInvalidate();
        }
    }
}
